package com.example.dsssx_library.response;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

//响应类
//提供独写方法的注解
@Data
public class Result {
    private Integer code;
    private String message;
    private boolean success;
    private Map<String, Object> data = new HashMap<>();

    //类外不让new
    private Result(){

    }

    public static Result ok(){
        Result result = new Result();
        result.setSuccess(true);
        result.setMessage(ResultCode.SUCCESS.getMessage());
        result.setCode(ResultCode.SUCCESS.getCode());
        return result;
    }

    //默认失败
    public static Result error(){
        Result result = new Result();
        result.setSuccess(false);
        result.setMessage(ResultCode.FAIL.getMessage());
        result.setCode(ResultCode.FAIL.getCode());
        return result;
    }

    //返回有数据的(键值对)
    public Result data(String key, Object value){
        this.data.put(key,value);
        return this;
    }

    //返回有数据的(Map)
    public Result data(Map<String, Object> data){
        this.data = data ;
        return this;
    }

    //有原因失败
    public Result error(ResultCode resultCode){
        Result result = new Result();
        result.setSuccess(false);
        result.setMessage(resultCode.FAIL.getMessage());
        result.setCode(resultCode.FAIL.getCode());
        return result;
    }

    //成功与否
    public Result success(Boolean bool){
        this.setSuccess(bool);
        return this;
    }

    public Result message(String message){
        this.setMessage(message);
        return this;
    }

    public Result code(Integer code){
        this.setCode(code);
        return this;
    }
}
